package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private IConsumptionItemService consumptionItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    // 处理结算单查询分页请求 /consumption/list
    @RequestMapping("/list")
    @RequiredPermission(name = "结算单查询", expression = "consumption:list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo) {
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        model.addAttribute("businesses", businessService.listAll());
        return "consumption/list"; // /WEB-INF/views/consumption/list.ftl
    }


    // 处理删除请求 /consumption/delete?id=1
    @RequestMapping("/delete")
    @RequiredPermission(name = "结算单删除", expression = "consumption:delete")
    public String delete(Long id) {

        if (id != null) {
            consumptionService.delete(id);
        }
        return "redirect:/consumption/list"; // localhost/consumption/list
    }

    // 处理保存或者修改请求 /consumption/saveOrUpdate 参数在请求体
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "结算单编辑(无预约单)", expression = "consumption:saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {

        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list"; // localhost/consumption/list
    }

    @RequestMapping("/save")
    @RequiredPermission(name = "结算单编辑(有预约单)", expression = "consumption:save")
    public String save(Long aid) {
        //预约单状态:消费中状态
        //新增预约的关联结算单
        Long cid = consumptionService.save(aid);
        return "redirect:/consumption/input?id=" + cid;
    }

    @RequestMapping("/input")
    @RequiredPermission(name = "结算单编辑", expression = "consumption:saveOrUpdate")
    public String input(Long id, Model model) {

        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption", consumption);

            //查询结算单明细
            List<ConsumptionItem> items = consumptionItemService.queryByCno(consumption.getCno());

            model.addAttribute("items", items);

            List<SystemDictionary> business = systemDictionaryService.queryBySn("business");
            List<SystemDictionary> settlements = systemDictionaryService.queryBySn("settlement_type");

            model.addAttribute("catalogs", business);
            model.addAttribute("settlements", settlements);

        }
        model.addAttribute("businesses", businessService.listAll());
        return "consumption/input";
    }

}
